package com.qf.drbackend.conf;

import com.fasterxml.jackson.databind.json.JsonMapper;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@Configuration
public class MVCConfig implements WebMvcConfigurer {

    /*
        自定义拦截器的注入
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        WebMvcConfigurer.super.addInterceptors(registry);
    }


    /*
        配置消息转换
     */
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        WebMvcConfigurer.super.configureMessageConverters(converters);
    }

    /*
        扩展原有的消息转换器
     */
    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        //在springmvc中，参数的接收，结果的返回，都是依赖于这个转换器完成序列化(java对象转成字符串)和反序列化的(把字符串转成java对象)
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        //给消息转换器设置我们自定义的ObjectMapper对象，完成自定义的数据类型的序列化和反序列化
        converter.setObjectMapper(new JacksonObjectMapper());

        //一定要将我们自定义的消息转换器加载最头部。覆盖原本的json消息转换器
        converters.add(0,converter);
    }
}
